package org.example.hansp.genetic.exercise;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
/*定义Employee类1)该类包含:private成员变量name,sal,birthday，其中 birthday 为 MyDate 类的对象
2)为每一个属性定义 getter, setter 方法;
3)重写 toString 方法输出 name, sal, birthday
4)MyDate类包含: private成员变量month,day,year;并为每一个属性定义 gettersetter 方法;
5)创建该类的3个对象，并把这些对象放入 ArrayList 集合中(ArrayList 需使用泛型来定义)，对集合中的元素进行排序，并遍历输出:
排序方式:调用ArrayList 的 sort 方法，传入Comparator对象[使用泛型]，先按照name排序，如果name相同，则按生日日期的先后排序。【即:定制排序】*/
public class GenericExercise01 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = getEmployees();
        employees.sort(new Comparator<Employee>() {
            public int compare(Employee e1, Employee e2) {
                if(!(e1 instanceof Employee||e2 instanceof Employee)||e1==null||e2==null){
                    System.out.println("类型不正确,或不能为空");
                    return 0;
                }
            /*  if(e1.getName().equals(e2.getName())){
                 return e1.getBirthday().comp(e1.getBirthday(), e2.getBirthday());
              }
              return e1.getName().compareTo(e2.getName());*/
                int nameComp = e1.getName().compareToIgnoreCase(e2.getName());
                if(nameComp!=0){
                    return nameComp;
                }
                return e1.getBirthday().compareTo(e2.getBirthday());
            }
        });
        System.out.println(employees);
    }

    private static ArrayList<Employee> getEmployees() {
        Employee employee1 = new Employee("lim",29000,new MyDate(2004,01,04));
        Employee employee2 = new Employee("lim",39000,new MyDate(2004,03,24));
        Employee employee3 = new Employee("philip",49000,new MyDate(2000,06,19));
        Employee employee4 = new Employee("alias",49000,new MyDate(2000,06,19));
        ArrayList<Employee> employees = new ArrayList<Employee>();
        employees.add(employee1);
        employees.add(employee2);
        employees.add(employee3);
        employees.add(employee4);
        return employees;
    }
}
class Employee{
    private String name;
    private double sal;
    private MyDate birthday;

    public String getName() {
        return name;
    }

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday='" + birthday + '\'' +
                '}'+"\n";
    }
}
class MyDate implements Comparable<MyDate>{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return
                "year=" + year +
                ", month=" + month +
                ", day=" + day ;
    }

    public int comp(MyDate d1, MyDate d2){
        if(d1.getYear() == d2.getYear() && d1.getMonth() == d2.getMonth()&& d1.getDay() == d2.getDay()){
            return 0;
        }
        if(d1.getYear()==d2.getYear()&&d1.getMonth()==d2.getMonth()){
            return d1.getDay()-d2.getDay();
        }
        if(d1.getYear()==d2.getYear()){
            return d1.getMonth()-d2.getMonth();
        }
       return d1.getYear()-d2.getYear();

    }

    @Override
    public int compareTo(MyDate o) {

        int yearComp=year-o.getYear();
        if(yearComp!=0){
            return yearComp;
        }
        int monthComp=month-o.getMonth();
        if(monthComp!=0){
            return monthComp;
        }
        return day-o.getDay();
    }
}
